public class HashBuck2<K,V> {
    static class Node<K,V>{
        K key;
        V val;
        Node<K,V> next;
        public Node(K key, V val){
            this.key = key;
            this.val = val;
        }

    }

    public Node<K,V>[] array = (Node<K,V>[])new Node[10];
    public int useSize;
    public static float LOAD_FACTOR = 0.75f;  //负载因子
    public void put(K key, V val){
        //1.通过哈希函数找到key存放的下标
        int hashCode = key.hashCode();
        int index = hashCode % array.length;

        //2.遍历链表 看看有没有和key相同的下标 如果有更新val
        Node<K,V> cur = array[index];
        while(cur != null){
            if(cur.key.equals(key)){
                cur.val = val;
            }
            cur = cur.next;
        }

        //3头插法插入node
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        useSize++;

        //4看负载因子有没有大于0.75
        if(dofactor() >= LOAD_FACTOR){
            resize();
        }

    }
    private float dofactor(){
        return useSize*1.0f / array.length;
    }

    private void resize(){
        Node<K,V>[] newArray = (Node<K,V>[])new Node[array.length * 2];
        for (int i = 0; i < array.length; i++) {
            Node<K,V> cur = array[i];
            while(cur != null){
                Node curN = cur.next;
                int hashCode = cur.key.hashCode();
                int newIndex = hashCode % newArray.length;
                cur.next = newArray[newIndex];
                newArray[newIndex] = cur;
                cur = curN;
            }
        }

    }



}

